package nowCoder;

public class MoreThanHalfNum_Solution {
	public int MoreThanHalfNum_Solution(int[] array) {
		if (array == null || array.length == 0)
			return 0;

		int flag = array[0];
		int count = 1;
		for (int i = 1; i < array.length; i++) {
			if (flag == array[i]) {
				count++;
			} else {
				count--;
				if (count == 0) {
					if (i + 1 < array.length) {
						flag = array[i + 1];
						count = 1;
						i++;
					} else {
						return 0;
					}
				}
			}
		}
		count = 0;
		for (int i = 0; i < array.length; i++)
			if (flag == array[i])
				count++;
		if (count > array.length / 2)
			return flag;
		else {
			return 0;
		}
	}

	public static void main(String[] args) {
		MoreThanHalfNum_Solution moreThanHalfNum_Solution = new MoreThanHalfNum_Solution();
		int[] array = { 1};
		System.out.println(moreThanHalfNum_Solution
				.MoreThanHalfNum_Solution(array));
	}
}
